<template>
  <div class="divBox">
    <el-card class="box-card">
      <div slot="header" class="clearfix">
        <div class="container">
          <el-form size="small" label-width="120px" :inline="true">
            <el-form-item label="商品状态：">
              <el-select v-model="tableFrom.status" placeholder="请选择" class="filter-item selWidth mr20" clearable
                @change="getList">
                <el-option label="上架" value="1" />
                <el-option label="下架" value="0" />
              </el-select>
            </el-form-item>
            <el-form-item label="关键字搜索：">
              <el-input v-model="tableFrom.keyword" placeholder="请输入商品名称，关键字，产品编号" class="selWidth"
                @keyup.enter.native="getList">
                <el-button slot="append" icon="el-icon-search" class="el-button-solt" @click="getList" />
              </el-input>
            </el-form-item>
          </el-form>
        </div>
        <router-link :to=" { path:`${roterPre}` + '/marketing/exclusive/add' } ">
          <el-button size="small" type="primary">
            <i class="add">+</i> 添加商品
          </el-button>
        </router-link>
      </div>
      <el-table v-loading="listLoading" :data="tableData.data" style="width: 100%" size="mini"
        :row-class-name="tableRowClassName" @rowclick.stop="closeEdit">
        <el-table-column prop="product_id" label="ID" min-width="50" />
        <el-table-column prop="product.store_name" label="商品名称" min-width="120" />
        <el-table-column label="商品图" min-width="80">
          <template slot-scope="scope">
            <div class="demo-image__preview">
              <el-image :src="scope.row.product.image" :preview-src-list="[scope.row.product.image]" />
            </div>
          </template>
        </el-table-column>
        <el-table-column prop="product.price" label="售价" min-width="90" />
        <el-table-column prop="once_pay_count" label="每人每日限购数量" min-width="90" />
        <el-table-column prop="sales" label="销量" min-width="90" />
        <el-table-column label="限量剩余" min-width="90">
          <template slot-scope="scope">
            <span>{{ scope.row.stock - scope.row.sales }}</span>
          </template>
        </el-table-column>
        <el-table-column prop="status" label="上/下架" min-width="80">
          <template slot-scope="scope">
            <el-switch v-model="scope.row.status" :active-value="1" :inactive-value="0" active-text="上架"
              inactive-text="下架" @change="onchangeIsShow(scope.row)" />
          </template>
        </el-table-column>
        <el-table-column prop="is_integral_exchange" label="是否开启积分抵扣" min-width="80">
          <template slot-scope="scope">
            <el-switch v-model="scope.row.is_integral_exchange" :active-value="1" :inactive-value="0" active-text="是"
              inactive-text="否" @change="onchangeExchange(scope.row)" />
          </template>
        </el-table-column>

        <el-table-column label="操作" min-width="150" fixed="right">
          <template slot-scope="scope">
            <router-link :to="{path: roterPre + '/marketing/exclusive/add/' + scope.row.product_id}">
              <el-button type="text" size="small" class="mr10">编辑</el-button>
            </router-link>
            <el-button type="text" size="small" class="mr10" @click="goDetail(scope.row.product_id)">详情</el-button>
          </template>
        </el-table-column>
      </el-table>
      <div class="block">
        <el-pagination :page-sizes="[20, 40, 60, 80]" :page-size="tableFrom.limit" :current-page="tableFrom.page"
          layout="total, sizes, prev, pager, next, jumper" :total="tableData.total" @size-change="handleSizeChange"
          @current-change="pageChange" />
      </div>
    </el-card>
    <el-dialog v-if="dialogVisible" title="商品详情" center :visible.sync="dialogVisible" width="700px">
      <div v-loading="loading" style="margin-top: 5px;">
        <div class="box-container">
          <div class="title">基本信息</div>
          <div class="acea-row">
            <div class="list sp"><label class="name">商品ID：</label>{{ formValidate.product_id }}</div>
            <div class="list sp"><label class="name">商品名称：</label><span>{{ formValidate.store_name }}</span></div>
            <div class="list sp100 image">
              <label class="name">商品图：</label>
              <img style="max-width: 150px; height: 80px;" :src="formValidate.image">
            </div>
            <div class="list sp100">
              <label class="name">商品信息</label>
              <!-- 单规格表格-->
              <div v-if="formValidate.spec_type === 0">
                <el-table :data="OneattrValue" border class="tabNumWidth" size="mini">
                  <el-table-column align="center" label="价格" min-width="80">
                    <template slot-scope="scope">
                      <span>{{ scope.row['price'] }}</span>
                    </template>
                  </el-table-column>
                  <el-table-column align="center" label="已售商品数量" min-width="80">
                    <template slot-scope="scope">
                      <span>{{ scope.row['sales'] }}</span>
                    </template>
                  </el-table-column>
                </el-table>
              </div>
              <!-- 多规格表格-->
              <div v-if="formValidate.spec_type === 1" class="labeltop" label="规格列表：">
                <el-table :data="ManyAttrValue" height="260" tooltip-effect="dark"
                  :row-key="(row) => { return row.id }">
                  <template v-if="manyTabDate">
                    <el-table-column v-for="(item,iii) in manyTabDate" :key="iii" align="center"
                      :label="manyTabTit[iii].title" min-width="80">
                      <template slot-scope="scope">
                        <span class="priceBox" v-text="scope.row[iii]" />
                      </template>
                    </el-table-column>
                  </template>
                  <el-table-column align="center" label="价格" min-width="80">
                    <template slot-scope="scope">
                      <span>{{ scope.row['price'] }}</span>
                    </template>
                  </el-table-column>
                  <el-table-column align="center" label="已售商品数量" min-width="80">
                    <template slot-scope="scope">
                      <span>{{ scope.row['sales'] }}</span>
                    </template>
                  </el-table-column>
                </el-table>
              </div>
            </div>
          </div>
          <div class="title" style="margin-top: 20px;">商品活动信息</div>
          <div class="acea-row">

            <div class="list sp100"><label class="name">个人最多购买次数：</label>{{ formValidate.once_pay_count }}次</div>
            <div class="list sp"><label class="name">状态：</label>{{ formValidate.status === 1 ? '上架' :  '下架' }}
            </div>
            <div class="list sp"><label
                class="name">是否开启积分抵扣：</label>{{ formValidate.is_integral_exchange === 1 ? '开启' :  '关闭' }}
            </div>

            <div class="list sp"><label class="name">创建时间：</label>{{ formValidate.create_time }}</div>
          </div>
        </div>
      </div>
    </el-dialog>
  </div>
</template>
<script>
  // +----------------------------------------------------------------------
  // | CRMEB [ CRMEB赋能开发者，助力企业发展 ]
  // +----------------------------------------------------------------------
  // | Copyright (c) 2016~2021 https://www.crmeb.com All rights reserved.
  // +----------------------------------------------------------------------
  // | Licensed CRMEB并不是自由软件，未经许可不能去掉CRMEB相关版权
  // +----------------------------------------------------------------------
  // | Author: CRMEB Team <admin@crmeb.com>
  // +----------------------------------------------------------------------
  import {
    freeProductLstApi,
    categorySelectApi,
    spikelstFilterApi,
    freeStatusApi,
    freeIntegralApi,
    categoryListApi,
    freeProductDetailApi
  } from '@/api/product'
  import {
    seckillProSort,
  } from '@/api/marketing'
  import previewBox from '@/components/previewBox/index'
  import {
    roterPre
  } from '@/settings'
  const defaultObj = {
    product_id: '',
    image: '',
    slider_image: [],
    store_name: '',
    store_info: '',
    start_day: '',
    end_day: '',
    start_time: '',
    end_time: '',
    start_day: '',
    end_day: '',
    is_open_recommend: 1,
    is_open_state: 1,
    is_show: 1,
    seckill_status: '',
    keyword: '',
    brand_id: '', // 品牌id
    cate_id: '', // 平台分类id
    mer_cate_id: [], // 商户分类id
    unit_name: '',
    integral: 0,
    sort: 0,
    is_good: 0,
    temp_id: '',
    seckill_date: '',
    finalPayment_date: '',
    delivery_type: 1,
    delivery_day: 10,
    create_time: '',
    attrValue: [{
      image: '',
      price: null,
      price: null,
      cost: null,
      ot_price: null,
      old_stock: null,
      stock: null,
      bar_code: '',
      weight: null,
      volume: null
    }],
    attr: [],
    extension_type: 0,
    content: '',
    spec_type: 0,
    is_gift_bag: 0,
    tattend_two: {},
    tattend_one: {}
  }

  export default {
    name: 'ProductList',
    components: {
      previewBox
    },
    data() {
      return {
        props: {
          emitPath: false
        },
        roterPre: roterPre,
        headeNum: [],
        listLoading: true,
        tableData: {
          data: [],
          total: 0
        },
        seckillStatusList: [{
            label: '正在进行',
            value: 2
          },
          {
            label: '活动已结束',
            value: 3
          },
          {
            label: '活动未开始',
            value: 1
          }
        ],
        productStatusList: [{
            label: '上架显示',
            value: 1
          },
          {
            label: '下架',
            value: 0
          },
          {
            label: '平台关闭',
            value: -1
          }
        ],
        tableFrom: {
          page: 1,
          limit: 20,
          mer_cate_id: '',
          cate_id: '',
          keyword: '',
          type: 4,
          seckill_status: '',
          us_status: '',
          product_id: this.$route.query.id ? this.$route.query.id : ''
        },
        categoryList: [], // 平台
        merCateList: [], // 商户分类筛选
        modals: false,
        dialogVisible: false,
        manyTabTit: {},
        manyTabDate: {},
        formValidate: Object.assign({}, defaultObj),
        OneattrValue: [Object.assign({}, defaultObj.attrValue[0])], // 单规格
        ManyAttrValue: [Object.assign({}, defaultObj.attrValue[0])], // 多规格
        attrInfo: {},
        tabClickIndex: '',
        previewVisible: false,
        goodsId: '',
        previewKey: ''
      }
    },
    mounted() {
      this.getLstFilterApi()
      this.getCategorySelect()
      this.getCategoryList()
      this.getList()
    },
    methods: {
      // 把每一行的索引放进row
      tableRowClassName({
        row,
        rowIndex
      }) {
        row.index = rowIndex
      },
      // 添加明细原因 row 当前行 column 当前列
      tabClick(row) {
        this.tabClickIndex = row.index
        //   this.tabClickLabel = column.label;
      },
      // 失去焦点初始化
      inputBlur(scope) {
        if (!scope.row.sort || scope.row.sort < 0) scope.row.sort = 0
        seckillProSort(scope.row.product_id, {
            sort: scope.row.sort
          })
          .then((res) => {
            this.closeEdit()
            //   this.$message.success(res.message);
          })
          .catch((res) => {
            //  this.$message.error(res.message);
          })
      },
      closeEdit() {
        this.tabClickIndex = null
      },
      // 添加淘宝商品成功
      onClose() {
        this.modals = false
      },

      // 商户分类；
      getCategorySelect() {
        categorySelectApi()
          .then((res) => {
            this.merCateList = res.data
          })
          .catch((res) => {
            this.$message.error(res.message)
          })
      },
      // 平台分类；
      getCategoryList() {
        categoryListApi()
          .then((res) => {
            this.categoryList = res.data
          })
          .catch((res) => {
            this.$message.error(res.message)
          })
      },
      // 列表表头；
      getLstFilterApi() {
        spikelstFilterApi()
          .then((res) => {
            this.headeNum = res.data
          })
          .catch((res) => {
            this.$message.error(res.message)
          })
      },
      watCh(val) {
        const tmp = {}
        const tmpTab = {}
        this.formValidate.attr.forEach((o, i) => {
          tmp['value' + i] = {
            title: o.value
          }
          tmpTab['value' + i] = ''
        })
        this.ManyAttrValue.forEach((val, index) => {
          const key = Object.values(val.detail).sort().join('/')
          if (this.attrInfo[key]) this.ManyAttrValue[index] = this.attrInfo[key]
        })
        this.attrInfo = {}
        this.ManyAttrValue.forEach((val) => {
          this.attrInfo[Object.values(val.detail).sort().join('/')] = val
        })
        this.manyTabTit = tmp
        this.manyTabDate = tmpTab
        // this.formThead = Object.assign({}, this.formThead, tmp);
        // console.log(this.formThead)
      },
      // 查看详情
      goDetail(id) {
        this.dialogVisible = true
        this.loading = true
        this.formValidate = {}
        freeProductDetailApi(id).then(async (res) => {
            this.loading = false
            const info = res.data
            this.formValidate = {
              product_id: info.product_id,
              image: info.image,
              slider_image: info.slider_image,
              store_name: info.store_name,
              store_info: info.store_info,
              brand_id: info.brand_id, // 品牌id
              cate_id: info.cate_id ? info.cate_id : '', // 平台分类id
              mer_cate_id: info.mer_cate_id, // 商户分类id
              unit_name: info.unit_name,
              sort: info.sort,
              is_good: info.is_good,
              temp_id: info.temp_id,
              is_show: info.is_show,
              attr: info.attr,
              extension_type: info.extension_type,
              content: info.content,
              spec_type: info.spec_type,
              is_gift_bag: info.is_gift_bag,
              create_time: info.create_time,
              product_status: info.product_status,
              status: info.status,
              all_pay_count: info.freeActive ? info.freeActive.all_pay_count : '',
              once_pay_count: info.freeActive ? info.freeActive.once_pay_count : '',
              stock: info.stock,
              stock_count: info.stock_count,
              seckill_status: info.seckill_status,
              us_status: info.us_status
            }
            if (this.formValidate.spec_type === 0) {
              this.OneattrValue = info.attrValue
            } else {
              this.ManyAttrValue = info.attrValue
              this.watCh(this.formValidate.attr)
            }
            console.log(this.ManyAttrValue)
            this.fullscreenLoading = false
            this.formValidate.seckill_date = [
              info.start_time,
              info.end_time
            ]
          })
          .catch((res) => {
            this.fullscreenLoading = false
            this.$message.error(res.message)
          })
      },

      // 列表
      getList() {
        this.listLoading = true
        freeProductLstApi(this.tableFrom)
          .then((res) => {
            this.tableData.data = res.data.list
            this.tableData.total = res.data.count
            this.listLoading = false
          })
          .catch((res) => {
            this.listLoading = false
            this.$message.error(res.message)
          })
      },
      pageChange(page) {
        this.tableFrom.page = page
        this.getList()
      },
      handleSizeChange(val) {
        this.tableFrom.limit = val
        this.getList()
      },

      //更改状态
      onchangeIsShow(row) {
        freeStatusApi(row.product_id, row.status)
          .then(({
            message
          }) => {
            this.$message.success(message)
            this.getList()
            this.getLstFilterApi()
          })
          .catch(({
            message
          }) => {
            this.$message.error(message)
          })
      },
      //是否开启积分抵扣
      onchangeExchange(row) {
        freeIntegralApi(row.product_id, row.is_integral_exchange)
          .then(({
            message
          }) => {
            this.$message.success(message)
            this.getList()
            this.getLstFilterApi()
          })
          .catch(({
            message
          }) => {
            this.$message.error(message)
          })
      },
    }
  }
</script>

<style scoped lang="scss">
  .bg {
    z-index: 100;
    position: fixed;
    left: 0;
    top: 0;
    width: 100%;
    height: 100%;
    background: rgba(0, 0, 0, 0.5);
  }

  .goods_detail .goods_detail_wrapper {
    z-index: -10
  }

  /deep/ table .el-input__inner {
    padding: 0;
  }

  .add {
    font-style: normal;
    position: relative;
    top: -1.2px;
  }

  .demo-table-expand {
    font-size: 0;
  }

  .demo-table-expand1 {
    /deep/ label {
      width: 77px !important;
      color: #99a9bf;
    }
  }

  .demo-table-expand .el-form-item {
    margin-right: 0;
    margin-bottom: 0;
    width: 33.33%;
  }

  .selWidth {
    width: 350px !important;
  }

  .seachTiele {
    line-height: 35px;
  }

  .box-container {
    overflow: hidden;
  }

  .box-container .list {
    float: left;
    line-height: 40px;
  }

  .box-container .sp {
    width: 50%;
  }

  .box-container .sp3 {
    width: 33.3333%;
  }

  .box-container .sp100 {
    width: 100%;
  }

  .box-container .list .name {
    display: inline-block;
    color: #606266;

  }

  .box-container .list .blue {
    color: #1890ff;
  }

  .box-container .list.image {
    margin: 20px 0;
    position: relative;
  }

  .box-container .list.image img {
    position: absolute;
    top: -20px;
  }

  .labeltop {
    max-height: 280px;
    overflow-y: auto;
  }

  .title {
    margin-bottom: 16px;
    color: #17233d;
    font-size: 14px;
    font-weight: bold;
    padding-bottom: 2px;
    border-bottom: 1px solid #dfe6ec;
  }
</style>
